﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Web;

namespace StateTheater.Helpers
{
    /// <summary>
    /// 
    /// </summary>
    class GravatarHelper
    {
        /// <summary>
        /// 
        /// </summary>
        public enum RatingType { G, PG, R, X }

        private const string defaultLinkText = "Create your own Gravatar here";

        // Instance overloads

        public string CreateImageTag(string email)
        {
            return GravatarHelper.CreateImageTag(email, 80, null, RatingType.G, true, defaultLinkText);
        }

        public string CreateImageTag(string email, int size)
        {
            return GravatarHelper.CreateImageTag(email, size, null, RatingType.G, true, defaultLinkText);
        }

        public string CreateImageTag(string email, int size, string defaultImage)
        {
            return GravatarHelper.CreateImageTag(email, size, defaultImage, RatingType.G, true, defaultLinkText);
        }

        public string CreateImageTag(string email, int size,RatingType maxAllowedRating,  string defaultImage, bool outputGravatarSiteLink, string linkTitle)
        {
            return GravatarHelper.CreateImageTag(email, size, defaultImage, maxAllowedRating, outputGravatarSiteLink, linkTitle);
        }

        /// <summary>
        /// Creates the image tag.
        /// </summary>
        /// <param name="email">The email.</param>
        /// <param name="size">The size.</param>
        /// <param name="defaultImage">The default image.</param>
        /// <param name="maxAllowedRating">The max allowed rating.</param>
        /// <param name="outputGravatarSiteLink">if set to <c>true</c> [output gravatar site link].</param>
        /// <param name="linkTitle">The link title.</param>
        /// <returns></returns>
        static public string CreateImageTag(string email, int size, string defaultImage, RatingType maxAllowedRating, bool outputGravatarSiteLink, string linkTitle)
        {
            if (string.IsNullOrEmpty(email))
                throw new ArgumentException("Email address must be specified", "email");

            if (size < 1 || size > 512)
                size = 80;

            // build up image url, including MD5 hash for supplied email:
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();

            UTF8Encoding encoder = new UTF8Encoding();
            MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();

            byte[] hashedBytes = md5Hasher.ComputeHash(encoder.GetBytes(email));
            StringBuilder sb = new StringBuilder(hashedBytes.Length * 2);

            for (int i = 0; i < hashedBytes.Length; i++)
            {
                sb.AppendFormat("{0:x2}", hashedBytes[i]);
            }
            string hash = sb.ToString();

            string format = "";
            if (string.IsNullOrEmpty(defaultImage))
            {
                if (outputGravatarSiteLink)
                    format = @"<a href=""http://www.gravatar.com"" title=""{4}""><img src=""http://www.gravatar.com/avatar.php?gravatar_id={0}&rating={1}&size={2}"" height=""{2}"" width=""{2}"" alt=""Gravatar"" /></a>";
                else
                    format = @"<img src=""http://www.gravatar.com/avatar.php?gravatar_id={0}&rating={1}&size={2}"" height=""{2}"" width=""{2}"" alt=""Gravatar"" />";
            }
            else
            {
                if (outputGravatarSiteLink)
                    format = @"<a href=""http://www.gravatar.com"" title=""{4}""><img src=""http://www.gravatar.com/avatar.php?gravatar_id={0}&rating={1}&size={2}&default={3}"" height=""{2}"" width=""{2}"" alt=""Gravatar"" /></a>";
                else
                    format = @"<img src=""http://www.gravatar.com/avatar.php?gravatar_id={0}&rating={1}&size={2}&default={3}"" height=""{2}"" width=""{2}"" alt=""Gravatar"" />";
            }

            return string.Format(format, sb, maxAllowedRating, size, HttpUtility.UrlEncode(defaultImage), linkTitle);
        }
    }
}